/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.mobbuzz.storage;

import edu.mobbuzz.bean.Label;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Vector;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
/**
 *
 * @author penyihirkecil
 */
public class LabelRecordStore {
    private final int MAX_LABEL = 100;
    private RecordStore rsLabel;
    public Label[] labelArr;
    private int nbLabel;

    private int tempInt;
    private byte []data;

    ByteArrayOutputStream bout;
    DataOutputStream dout;


    public LabelRecordStore(){
        /*try {
            RecordStore.deleteRecordStore("Contacts");
        } catch (RecordStoreException ex) {
            ex.printStackTrace();

        }*/
        openRecStore();
    }

    public Label getLabel (int recId){
        for (tempInt=0;tempInt<getNbLabel();tempInt++) {
            if (labelArr[tempInt].getRecId() == recId){
                return labelArr[tempInt];
            }
                
        }
        return null;
    }

    public Vector getLabelByRss (int rssRecId){
        Vector labelVector = new Vector();
        if(readRSLabel()){
            for (tempInt=0;tempInt<getNbLabel();tempInt++) {
                
                if(labelArr[tempInt]!=null){
                   if (labelArr[tempInt].getRecIdRSS() == rssRecId){                       
                       labelVector.addElement(labelArr[tempInt]);
                   }
                }
            }
        }
        
        return labelVector;
    }

    public Vector getLabelByCategory (int categoryRecId){
        Vector labelVector = new Vector();
        if(readRSLabel()){
            for (tempInt=0;tempInt<getNbLabel();tempInt++) {
                
                if(labelArr[tempInt]!=null){
                   if (labelArr[tempInt].getRecIdRSS() == categoryRecId){
                       labelVector.addElement(labelArr[tempInt]);
                   }
                }
            }
        }

        return labelVector;
    }

    public Vector getDeletedIndexLabelByCategory (int categoryRecId){
        Vector labelVector = new Vector();
        if(readRSLabel()){
            for (tempInt=0;tempInt<getNbLabel();tempInt++) {                
                if(labelArr[tempInt]!=null){
                   if (labelArr[tempInt].getRecIdCategory() == categoryRecId){
                       labelVector.addElement(new Integer(tempInt));
                   }
                }
            }
        }

        return labelVector;
    }

    public Vector getDeletedIndexLabelByRssAndCategory (int rssRecId, int categoryRecId){
        Vector labelVector = new Vector();
        if(readRSLabel()){
            for (tempInt=0;tempInt<getNbLabel();tempInt++) {
                if(labelArr[tempInt]!=null){
                   if (labelArr[tempInt].getRecIdRSS() == rssRecId && labelArr[tempInt].getRecIdCategory() == categoryRecId){
                       labelVector.addElement(new Integer(tempInt));
                   }
                }
            }
        }
        return labelVector;
    }
    
    public boolean isLabelByRssAndCategory (int rssRecId, int categoryRecId){        
        boolean success = false;
        if(readRSLabel()){
            for (tempInt=0;tempInt<getNbLabel();tempInt++) {
                if(labelArr[tempInt]!=null){
                   if (labelArr[tempInt].getRecIdRSS() == rssRecId && labelArr[tempInt].getRecIdCategory() == categoryRecId){
                       success = true;
                   }
                }
            }
        }else{
            success = false;
        }
        return success;
    }

    public Label getLabelByRssAndCategory (int rssRecId, int categoryRecId){        
        if(readRSLabel()){
            for (tempInt=0;tempInt<getNbLabel();tempInt++) {
                if(labelArr[tempInt]!=null){
                   if (labelArr[tempInt].getRecIdRSS() == rssRecId && labelArr[tempInt].getRecIdCategory() == categoryRecId){
                       return labelArr[tempInt];
                   }
                }
            }
        }else{
           return null;
        }
        return null;
    }

    public int getIndex (int recId){
        if(readRSLabel()){
            for (tempInt=0;tempInt<getNbLabel();tempInt++) {
                if (labelArr[tempInt].getRecId() == recId){
                    return tempInt;
                }
            }
        }
        return 0;
    }
    
    public boolean openRecStore () {
        try {
            rsLabel = RecordStore.openRecordStore("LABEL", true);
            labelArr = new Label[MAX_LABEL];
            setNbLabel(getRsLabel().getNumRecords());
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
        return true;
    }

    
    public boolean readRSLabel() {
        try {
            RecordEnumeration reLabel = getRsLabel().enumerateRecords (null,new RELabelSorter(),false);
            reLabel.reset();
            tempInt = 0;
            while (reLabel.hasNextElement()) {
                labelArr[tempInt] = new Label();
                readLabel(reLabel.nextRecordId(), labelArr[tempInt++]);
            }
            reLabel.destroy();
        } catch (RecordStoreException e) {
                e.printStackTrace();
                return false;
        }
        return true;
    }

    public void addLabel(Label label){
        if (getNbLabel() < MAX_LABEL) {
            try {
                bout = new ByteArrayOutputStream();
                dout = new DataOutputStream(bout);
                dout.writeInt(label.getRecIdRSS());
                dout.writeInt(label.getRecIdCategory());
                dout.flush();
                data = bout.toByteArray();
                bout.reset();
                labelArr[nbLabel] = new Label();
                labelArr[nbLabel].setRecId(getRsLabel().addRecord( data, 0, data.length ));
                labelArr[nbLabel].setRecIdRSS(label.getRecIdRSS());
                labelArr[nbLabel].setRecIdCategory(label.getRecIdCategory());
                
            } catch( Exception e ) {
                if (e instanceof RecordStoreFullException){

                }
                e.printStackTrace ();
            }
        } 
    }

    public void updateLabel(int index, Label label) {
        try {
            dout.writeInt(label.getRecIdRSS());
            dout.writeInt(label.getRecIdCategory());
            dout.flush();
            data = bout.toByteArray();
            getRsLabel().setRecord(label.getRecId(), data, 0, data.length );
            bout.reset();
            labelArr[index].setRecIdRSS(label.getRecIdRSS());
            labelArr[index].setRecIdCategory(label.getRecIdCategory());
           
        } catch( Exception e ) {
            e.printStackTrace();
        }
    }

    public void deleteLabel(int index) {
        try {
            getRsLabel().deleteRecord(labelArr[index].getRecId());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        setNbLabel(getNbLabel() - 1);
        for (tempInt=index; tempInt<getNbLabel(); tempInt++) {
            labelArr[tempInt] = labelArr[tempInt+1];
        }
        
    }

    public boolean readLabel(int id, Label label){
        data = new byte[110];
        ByteArrayInputStream bin = new ByteArrayInputStream(data);
        DataInputStream din = new DataInputStream(bin);

        if( getRsLabel() != null ){
            try {
                getRsLabel().getRecord( id, data, 0 );
                label.setRecId(id);
                label.setRecIdRSS(din.readInt());
                label.setRecIdCategory(din.readInt());
                din.reset();
            } catch( Exception e ) {
                return false;
            }
        }
        return true;

    }
    

    public void closeRecStore () {
        try {
            if( getRsLabel() != null )
                getRsLabel().closeRecordStore();
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }
    }

    public int getNbLabel() {
        return nbLabel;
    }

    public void setNbLabel(int nbLabel) {
        this.nbLabel = nbLabel;
    }

    /**
     * @return the rsLabel
     */
    public RecordStore getRsLabel() {
        return rsLabel;
    }
}
class RELabelSorter implements RecordComparator {

    public int compare(byte[] rec1, byte[] rec2) {

        ByteArrayInputStream  bin = new ByteArrayInputStream(rec1);
        DataInputStream       din = new DataInputStream(bin);
        String  name1, name2;

        try{
            name1 = din.readUTF();
            bin = new ByteArrayInputStream( rec2 );
            din = new DataInputStream( bin );

            name2 = din.readUTF();
            int cmp = name1.compareTo( name2);

            if( cmp != 0 )
                return ( cmp < 0 ? PRECEDES : FOLLOWS );
        }
        catch(Exception e){
            e.printStackTrace();
        }

        return EQUIVALENT;
    }

};
